package com.birdboot.controller;

import com.birdboot.annotations.Controller;
import com.birdboot.annotations.RequestMapping;
import com.birdboot.entity.User;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;
import com.birdboot.utils.DBUtil;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 处理与用户相关业务的类
 */
@Controller
public class UserController {
    private static File userDir;

    static {
        userDir = new File("./users");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }

    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户注册!!!!");
        /*
            1:通过请求对象获取表单数据
            2:将数据写入磁盘
            3:通过响应对象给浏览器发送处理结果
         */
        //1获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);
        if(username==null||username.isEmpty()||
           password==null||password.isEmpty()||
           nickname==null||nickname.isEmpty()||
           ageStr==null||ageStr.isEmpty()||
           !ageStr.matches("[0-9]{1,3}")){
            //要求浏览器重定向到reg_info_error.html
            response.sendRedirect("/reg_info_error.html");
            return;
        }

        //2
        int age = Integer.parseInt(ageStr);
        /*
            使用数据库实现注册功能
         */
        try (Connection connection = DBUtil.getConnection();){
            // 确认用户名是否被占用
            String sql1 = "SELECT id FROM tedu.userinfo WHERE username=?";
            PreparedStatement ps1 = connection.prepareStatement(sql1);
            ps1.setString(1, username);
            ResultSet resultSet = ps1.executeQuery();

            if (resultSet.next()){
                //System.out.println("用户名被占用");
                response.sendRedirect("/have_user.html");
            }else{
                // 执行注册流程
                String sql2 = "INSERT INTO tedu.userinfo(username,password,nickname,age) VALUES(?,?,?,?)";
                PreparedStatement ps2 = connection.prepareStatement(sql2);
                ps2.setString(1, username);
                ps2.setString(2, password);
                ps2.setString(3, nickname);
                ps2.setInt(4, age);
                // 执行DML
                int i = ps2.executeUpdate();
                System.out.println("恭喜你,注册成功");
                // 跳转到注册成功页面
                response.sendRedirect("/reg_success.html");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理用户登录....");
        //1获取表单信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username==null||username.isEmpty()||
           password==null||password.isEmpty()){
            response.sendRedirect("/login_info_error.html");
            return;
        }

        //2
        /*
            使用数据库处理登录功能
         */
        try (Connection connection = DBUtil.getConnection();){
            //1.创建预编译SQL执行对象: ?代表占位
            String sql = "SELECT username,password,nickname,age FROM tedu.userinfo WHERE username=? AND password=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 2.设置 ? 的值
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            // 3.执行[注意:不需要放参数-SQL语句]
            ResultSet resultSet = preparedStatement.executeQuery();
            // 4.判断结果
            if (resultSet.next()){
                // 登录成功,跳转到登录成功的页面
                response.sendRedirect("/login_success.html");
            }else{
                // 登录失败,跳转到登录失败的页面
                response.sendRedirect("/login_fail.html");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @RequestMapping("/userList")
    public void userList(HttpServletRequest request,HttpServletResponse response){

    }
}





